package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.backlist.CardBlackListAllDownloadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.backlist.VehicleBlackListIncrDownloadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.graylist.vehicle.VehicleGrayListAllDownloadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.graylist.vehicle.VehicleGrayListIncrDownloadRequest;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.OperlogDao;
import io.renren.modules.basedata.entity.tbl_Operlog;
import io.renren.modules.basedata.service.VehicleBlackListDownloadService;
import io.renren.modules.basedata.service.VehicleGrayListDownloadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
@Slf4j
public class VehicleGrayListDownloadServiceImpl implements VehicleGrayListDownloadService {

    /**
     * @author WANGYP
     * @version 实现车牌灰名单全量下载
     */
    @Override
    public String vehicleGrayListDownload() throws Exception {


        String result=null;

        tbl_Operlog operlog = new tbl_Operlog();

        Date datetime = new Date();

        String savePath = "";

        String version = "";

        int operResult=0;

        version = DateUtil.formatDateTime1(datetime).substring(0, 8);

        VehicleGrayListAllDownloadRequest request = new VehicleGrayListAllDownloadRequest();

        request.setVersion(version);

        String fileName = ETCCommHelper.getFileName(request,"65");

        try{

            File file =  ETCCommHelper.download(fileName,request,"D:\\");

            System.out.println("下载成功，下载文件为："+file.getAbsolutePath());

            if(file!=null){//下载成功
                /**
                 * 解析文件并处理数据
                 * **/
                //1、移动文件至工作目录
                //2、解压文件
                //3、获取解压后的版本的文件
                //4、顺序执行然后解压每个版本的各发行方文件
                //5、循环执行个各发行方并插入数据库

            }else{//下载失败
                System.out.println("下载失败");
            }

        }catch (IOException e) {
            //TODO 网络异常处理,各省补全
            e.printStackTrace();
        }catch (ApiRequestException apie) {
            //TODO 系统自定义异常处理，各省补全
            apie.printStackTrace();
            System.out.println("错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }

        return "车牌灰名单全量下载结束...";


    }

    /**
     * @author WANGYP
     * @version 实现车牌灰名单增量下载
     */
    @Override
    public String vehicleGrayListIncreDownload() throws Exception {


        log.info("开始车牌灰名单增量下载");

        String result=null;

        tbl_Operlog operlog = new tbl_Operlog();

        Date datetime = new Date();


        String version = "";

        int operResult=0;

        version = DateUtil.formatDateTime1(datetime).substring(0, 12);

        VehicleGrayListIncrDownloadRequest request = new VehicleGrayListIncrDownloadRequest();

        request.setVersion(version);

        String fileName = ETCCommHelper.getFileName(request,"65");

        try{

            File file =  ETCCommHelper.download(fileName,request,"D:\\");

            System.out.println("下载成功，下载文件为："+file.getAbsolutePath());

            if(file!=null){//下载成功
                /**
                 * 解析文件并处理数据
                 * **/
                //1、移动文件至工作目录
                //2、解压文件
                //3、获取解压后的版本的文件
                //4、顺序执行然后解压每个版本的各发行方文件
                //5、循环执行个各发行方并插入数据库

            }else{//下载失败
                System.out.println("下载失败");
            }

        }catch (IOException e) {
            //TODO 网络异常处理,各省补全
            e.printStackTrace();
        }catch (ApiRequestException apie) {
            //TODO 系统自定义异常处理，各省补全
            apie.printStackTrace();
            System.out.println("错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
        return "车牌灰名单增量下载结束...";


    }
}
